package com.situ.car.dao.Impl;

import com.situ.car.dao.ICarBrandDao;
import com.situ.car.pojo.entity.CarBrand;
import com.situ.car.pojo.entity.Role;
import com.situ.car.pojo.vo.BrandCountVo;
import com.situ.car.pojo.vo.OrderBrandCountVo;
import com.situ.car.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class CarBrandDaoImpl implements ICarBrandDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<CarBrand> selectAll() {
        return template.query("select * from car_brand", new BeanPropertyRowMapper<>(CarBrand.class));
    }

    @Override
    public void add(CarBrand carBrand) {
        System.out.println(carBrand);
        template.update("INSERT INTO car_brand (brand_name, brand_image) VALUES (?,?)", carBrand.getBrandName(),carBrand.getBrandImage());

    }

    @Override
    public Integer totalCost() {
        Integer count = template.queryForObject("select count(*) from car_brand", Integer.class);
        return count;
    }

    @Override
    public List<BrandCountVo> selectBrandCount() {
        return template.query("SELECT cb.brand_name AS name, COUNT(c.id) AS value\n" +
                "FROM car AS c\n" +
                "INNER JOIN car_brand AS cb ON c.brand_id = cb.brand_id\n" +
                "GROUP BY cb.brand_name;", new BeanPropertyRowMapper<>(BrandCountVo.class));
    }

    @Override
    public List<OrderBrandCountVo> selectOrderBrandCount() {
        return template.query("SELECT cb.brand_name AS name, COUNT(ro.id) AS value\n" +
                "FROM car_brand AS cb\n" +
                "LEFT JOIN car AS c ON cb.brand_id = c.brand_id\n" +
                "LEFT JOIN rental_order AS ro ON c.id = ro.car_id\n" +
                "GROUP BY cb.brand_name;", new BeanPropertyRowMapper<>(OrderBrandCountVo.class));
    }
}
